<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">用户登录</h2>
      <el-form :model="form" @submit.prevent="onLogin">
        <el-form-item label="账号">
          <el-input v-model="form.username" placeholder="请输入账号" autocomplete="username" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" autocomplete="current-password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onLogin" :loading="loading" style="width:100%">登录</el-button>
        </el-form-item>
        <el-form-item v-if="error">
          <el-alert :title="error" type="error" show-icon />
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

const form = ref({
  username: '',
  password: ''
})
const loading = ref(false)
const error = ref('')

const onLogin = async () => {
  error.value = ''
  loading.value = true
  // 这里模拟登录请求，实际应调用后端API
  if (form.value.username === 'xxk' && form.value.password === 'tK123') {
    ElMessage.success('登录成功')
    
    // 设置登录状态
    // 例如：localStorage.setItem('token', 'xxx'); router.push('/')

    //假设登录成功后设置一个登录状态，可以使用全局状态管理或其他方式
    //这里简单使用localStorage存储登录状态 
    sessionStorage.setItem('isLogin', 'true')    
    //跳转到HmoePage界面，并将登录状态传给登录界面的isLogin
    window.location.href = '/'  // 简单跳转到首页 

  } else {
    error.value = '账号或密码错误'
  }
  loading.value = false
}
</script>

<style scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 90vh;
  background: #f5f7fa;
}
.login-card {
  width: 350px;
  padding: 30px 20px; /* 调整内边距 */
}
.login-title {
  text-align: center;
  margin-bottom: 20px; /* 调整标题间距 */
}
</style>